> install.packages("jcolors",repos = "http://cran.us.r-project.org")
Installing package into 'C:/Users/anast/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
package 'jcolors' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\anast\AppData\Local\Temp\RtmpWA0bYk\downloaded_packages
> install.packages("viridisLite",repos = "http://cran.us.r-project.org")
Installing package into 'C:/Users/anast/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
package 'viridisLite' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\anast\AppData\Local\Temp\RtmpWA0bYk\downloaded_packages
> install.packages("viridis",repos = "http://cran.us.r-project.org")
Installing package into 'C:/Users/anast/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
package 'viridis' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\anast\AppData\Local\Temp\RtmpWA0bYk\downloaded_packages
> install.packages("ggmap",repos = "http://cran.us.r-project.org")
Installing package into 'C:/Users/anast/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
package 'ggmap' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\anast\AppData\Local\Temp\RtmpWA0bYk\downloaded_packages
> library("tidyverse")
-- Attaching packages ---------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
v ggplot2 3.1.1 v purrr 0.3.2
v tibble 2.1.1 v dplyr 0.8.0.1
v tidyr 0.8.3 v stringr 1.4.0
v readr 1.3.1 v forcats 0.4.0
-- Conflicts ------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
> library("ggmap")
Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
Please cite ggmap if you use it! See citation("ggmap") for details.
> library("jcolors")
> library("viridisLite")
> library("viridis")
select() to only subset the columns we will be looking at.> crime <- select(crime, Crime.Name1:Crime.Name3, Latitude, Longitude)
> head(crime)
# A tibble: 6 x 5
Crime.Name1 Crime.Name2 Crime.Name3 Latitude Longitude
<fct> <fct> <fct> <dbl> <dbl>
1 Crime Agains~ Counterfeitin~ FORGERY OF C~ 39.1 -77.1
2 Crime Agains~ Forcible Rape RAPE - STRON~ 39.1 -77.1
3 Crime Agains~ Burglary/Brea~ BURGLARY - F~ 39.2 -77.2
4 Crime Agains~ Destruction/D~ DAMAGE PROPE~ 39.2 -77.2
5 Crime Agains~ Drug/Narcotic~ DRUGS - OPIU~ 39.2 -77.3
6 Crime Agains~ Drug Equipmen~ DRUGS - NARC~ 39.2 -77.3
na.omit() and filter() functions, respectively.> crime_filtered <- na.omit(crime)
> crime_filtered <- filter(crime_filtered, Crime.Name1 == "Other" | Crime.Name1 == "Crime Against Property" | Crime.Name1 == "Crime Against Society" |Crime.Name1 == "Not a Crime" | Crime.Name1 == "Crime Against Person")
ggplot() and choose the data we will be working with, it will create an empty plot. Even though we specified what data we will be working with, we haven’t told ggplot what to do with that data.> ggplot(crime_filtered)
geom_bar() and geom_col(). The two functions are similar, however, bars use count as a y-axis value, while columns allow us to specify y-axis value manually.> ggplot(crime_filtered) + geom_bar(mapping=aes(x=Crime.Name1, fill = Crime.Name1))
> ggplot(crime_filtered) + geom_bar(mapping=aes(x=Crime.Name1, fill = Crime.Name1)) +
+ theme(plot.background = element_blank()) +
+ theme(panel.background = element_blank())
> ggplot(crime_filtered) + geom_bar(mapping=aes(x=Crime.Name1, fill = Crime.Name1)) +
+ theme(plot.background = element_blank()) +
+ theme(panel.background = element_blank()) +
+ theme(panel.grid.major.y = element_line(color="grey"))
scale_x_discrete() function, whereas y-axis is continuous and will require scale_y_continuous(). We then can change the names and add a limit for y-axis.> ggplot(crime_filtered) + geom_bar(mapping=aes(x=Crime.Name1, fill = Crime.Name1)) +
+ theme(plot.background = element_blank()) +
+ theme(panel.background = element_blank()) +
+ theme(panel.grid.major.y = element_line(color="grey")) +
+ scale_x_discrete(name = "Type of Crime") +
+ scale_y_continuous(name = "Number of Crimes", limits = c(0, 30000))
> ggplot(crime_filtered) + geom_bar(mapping=aes(x=Crime.Name1, fill = Crime.Name1)) +
+ theme(plot.background = element_blank()) +
+ theme(panel.background = element_blank()) +
+ theme(panel.grid.major.y = element_line(color="grey")) +
+ scale_x_discrete(name = "Type of Crime") +
+ scale_y_continuous(name = "Number of Crimes", limits = c(0, 30000)) +
+ scale_fill_jcolors("pal10", guide = guide_legend(title = "Type of Crime", nrow = 1, label.position = "bottom", keywidth = 2.5)) +
+ theme(legend.position = "bottom")
> ggplot(crime_filtered) + geom_bar(mapping=aes(x=Crime.Name1, fill = Crime.Name1)) +
+ theme(plot.background = element_blank()) +
+ theme(panel.background = element_blank()) +
+ theme(panel.grid.major.y = element_line(color="grey")) +
+ scale_x_discrete(name = "Type of Crime") +
+ scale_y_continuous(name = "Number of Crimes", limits = c(0, 30000)) +
+ scale_fill_jcolors("pal10", guide = guide_legend(title = "Type of Crime", nrow = 1, label.position = "bottom", keywidth = 2.5)) +
+ theme(legend.position = "bottom") +
+ ggtitle("Crime in Montgomery County, MD",
+ subtitle = "Source: catalog.data.gov/dataset/crime")
geom_point().> ggplot() +
+ geom_point(data = crime_filtered, aes(x = Longitude, y = Latitude))
alpha aestetic to our plot.> ggplot() +
+ geom_point(data = crime_filtered, aes(x = Longitude, y = Latitude), alpha = .04)
> register_google(key="AIzaSyDMpBxrUhyXmi6a7qTFazdZUS59MiFaoBk")
geocode() to geocode the coordinates, get_map() retrieves the map, and ggmap() plots it. Let’s geocode Montgomery County, MD.> montgomerycounty <- geocode("Montgomery County, MD")
Source : https://maps.googleapis.com/maps/api/geocode/json?address=Montgomery+County,+MD&key=xxx
> montgomerycounty
# A tibble: 1 x 2
lon lat
<dbl> <dbl>
1 -77.2 39.2
get_map() function and specifying the location as well. We can also change the maptype from the default ‘terrain’ to ‘hybrid’ and change the zoom.> map_montgomerycounty <- get_map("Montgomery County, MD", maptype = "hybrid", zoom = 10)
Source : https://maps.googleapis.com/maps/api/staticmap?center=Montgomery%20County,%20MD&zoom=10&size=640x640&scale=2&maptype=hybrid&language=en-EN&key=xxx
Source : https://maps.googleapis.com/maps/api/geocode/json?address=Montgomery+County,+MD&key=xxx
> ggmap(map_montgomerycounty)
> ggmap(get_map(montgomerycounty)) +
+ geom_point(mapping = aes(x=Longitude, y= Latitude), data = crime_filtered)
Source : https://maps.googleapis.com/maps/api/staticmap?center=39.154743,-77.240515&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
ggmap() on ‘map_montgomerycounty’ and change the color to red so is easier to see it on the hybrid map. We can also modify the transparency.> ggmap(map_montgomerycounty) +
+ geom_point(data = crime_filtered, aes(x = Longitude, y = Latitude), alpha = .04, color = "red")
> ggplot() +
+ stat_density2d(data = crime_filtered, aes(x = Longitude, y = Latitude, fill = ..density..), geom = 'tile', contour = F)
> ggmap(map_montgomerycounty) +
+ stat_density2d(data = crime_filtered, aes(x = Longitude, y = Latitude, fill = ..density..), geom = 'tile', contour = F, alpha = .5)
> ggmap(map_montgomerycounty) +
+ stat_density2d(data = crime_filtered, aes(x = Longitude, y = Latitude, fill = ..density..), geom = 'tile', contour = F, alpha = .5) +
+ scale_fill_viridis(option = "inferno") +
+ labs(title = "Crime in Montgomery County, MD",
+ subtitle = "Source: catalog.data.gov/dataset/crime",
+ fill = "Number of Crimes")